package com.junzichangsheng.rename;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Rename {
    private String SRC_PATH;
    private String DEST_PATH;
    private String PREFIX;
    private int DIGIT;

    public Rename(String SRC_PATH, String DEST_PATH, String PREFIX, int DIGIT) {
        this.SRC_PATH = SRC_PATH;
        this.DEST_PATH = DEST_PATH;
        this.PREFIX = PREFIX;
        this.DIGIT = DIGIT;
    }

    public void rename() throws IOException {
        File folder = new File(SRC_PATH); // 文件夹路径
        if(folder.isDirectory()) {
            File[] files = folder.listFiles();
            if(files != null) {
                int i = 1;
                for(File srcFile : files) {
                    if(srcFile.isFile()) {
                        String suffix = srcFile.getName().substring(srcFile.getName().lastIndexOf(".") + 1);
                        File destFile = new File(DEST_PATH + "\\" + PREFIX + String.format("%0" + DIGIT + "d", i) + "." + suffix);
                        FileUtils.moveFile(srcFile, destFile);
                        i++;
                    }
                }
            }
        }
    }
}
